package cn.software.service.impl;

import cn.software.entity.Subject;
import cn.software.entity.param.SubjectParam;
import cn.software.entity.vo.SubjectVo;
import cn.software.service.SubjectService;
import cn.software.mapper.SubjectMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 22184
* @description 针对表【subject(学科)】的数据库操作Service实现
* @createDate 2023-05-30 17:13:35
*/
@Service
public class SubjectServiceImpl implements SubjectService {

    @Autowired
    private SubjectMapper subjectMapper;

    private Subject transformSubjectParamToSubject(SubjectParam subjectParam) {
        Subject subject = new Subject();
        BeanUtils.copyProperties(subjectParam, subject);
        return subject;
    }

    @Override
    public boolean addSubject(SubjectParam subjectParam) {
        Subject subject = transformSubjectParamToSubject(subjectParam);
        int insert=subjectMapper.insert(subject);
        return insert>0;
    }

    @Override
    public boolean updateSubject(SubjectParam subjectParam){
        Subject subject=transformSubjectParamToSubject(subjectParam);
        int update=subjectMapper.updateById(subject);
        return update>0;
    }

    @Override
    public boolean deleteSubject(SubjectParam subjectParam){
        Subject subject=transformSubjectParamToSubject(subjectParam);
        subject.setIsDeleted(1);
        int delete=subjectMapper.updateById(subject);
        return delete>0;
    }

    @Override
    public List<Subject> getSubject(){
        QueryWrapper<Subject> qw=new QueryWrapper<>();
        qw.eq("is_deleted",0);
        List<Subject> list=subjectMapper.selectList(qw);
        return list;
    }

    @Override
    public SubjectVo getSubjectById(Integer subjectId) {
        Subject subject = subjectMapper.selectById(subjectId);
        if(subject == null || subject.getIsDeleted() == 1) {
            return null;
        }
        SubjectVo subjectVo = transformSubjectToVo(subject);
        return subjectVo;
    }

    private SubjectVo transformSubjectToVo(Subject subject) {
        SubjectVo subjectVo = new SubjectVo();
        BeanUtils.copyProperties(subject, subjectVo);
        return subjectVo;
    }


}
